<template>
  <div>

    <el-card>
      <div style="margin: 10px 5px;font-size: 17px ;cursor: pointer; color: cornflowerblue " class="item">
        {{ tableData.name }}
      </div>
      <div style="margin: 10px 5px ; font-size: 14px"><i class="el-icon-user"
                                                         style="margin-right: 5px"></i><span>{{ tableData.user }}</span>
      </div>
      <div style="margin: 10px 5px ; font-size: 14px"><i class="el-icon-time"
                                                         style="margin-right: 5px"></i><span>{{ tableData.time }}</span>
      </div>
    </el-card>
    <el-card>
      <div>
        <mavon-editor
            class="md"
            :value="content"
            :subfield="false"
            :defaultOpen="'preview'"
            :toolbarsFlag="false"
            :editable="false"
            :scrollStyle="true"
            :ishljs="true"
        />

      </div>
    </el-card>
    <el-card style="margin: 30px 0">
      <div style="font-size: 20px">评论</div>
      <el-divider></el-divider>
      <el-input type="textarea" v-model="CommentForm.content"></el-input>
      <div style="text-align: right" class="pd-10">
        <el-button type="primary" @click="userpinglun()">评论</el-button>
      </div>
      <el-divider></el-divider>
      <div style="font-size: 20px">评论列表</div>

      <!--        评论列表-->
      <div v-for="item in comments" class="pd-10">
        <!--  头部      -->
        <div style="text-align: center">
          <div style="height: 30px;align-items: center">
            <span style="margin-right: 5px"> 作者：{{ item.nickname }}</span>
            头像：<img :src="item.avatarUrl" alt=""
                    style="width: 30px; border-radius: 50%; position: relative; top: 10px; right: 5px">
            评论时间：{{ item.time }}
          </div>
        </div>

        <!-- 评论区         -->
        <div style="background-color: #eeeeee;margin-top: 15px;text-indent:2em;" class="pd-10">
          <div style="margin: 15px 25px;font-size: 20px;">{{ item.content }}
            <div style="margin-top: 10px;font-size: 12px;text-align: right">
              <el-button type="text" @click="tosave(item)">评论</el-button>
              <template slot-scope="scope">
              <el-popconfirm
                  class="ml-5"
                  confirm-button-text='确定'
                  cancel-button-text='我再想想'
                  icon="el-icon-info"
                  icon-color="red"
                  title="您确定删除吗？"
                  @confirm="del(scope.row.id)"
              >
              <el-button style="color: red" type="text"  v-if="user.id==item.userId" slot="reference">删除</el-button>
              </el-popconfirm>
              </template>
            </div>

            <el-divider></el-divider>
            <!-- 多级评论              -->
            <div v-if="item.children.length" v-for="childrenItem in item.children">
              <!--           二级评论     -->
              <div style="text-align: left;font-size: 13px">
                <div style="height: 30px;align-items: center">
                  <span style="margin-right: 5px"> 回复人：{{ childrenItem.nickname }}</span>
                  头像：<img :src="childrenItem.avatarUrl" alt=""
                          style="width: 30px; border-radius: 50%; position: relative; top: 10px; right: 5px">
                  评论时间：{{ childrenItem.time }}
                </div>
                <div>@{{childrenItem.pnickname}}</div>
              </div>
              <div>
                <div style="margin: 15px 25px;font-size: 16px;">{{ childrenItem.content }}
                  <div>
                    <div style="margin-top: 10px;font-size: 12px;text-align: right">
                      <el-button type="text" @click="tosave(childrenItem)">评论</el-button>
                      <template slot-scope="scope">
                      <el-popconfirm
                          class="ml-5"
                          confirm-button-text='确定'
                          cancel-button-text='我再想想'
                          icon="el-icon-info"
                          icon-color="red"
                          title="您确定删除吗？"
                          @confirm="del(scope.row.id)"
                      >
                      <el-button style="color: red" type="text"  v-if="user.id==childrenItem.userId" slot="reference">删除</el-button>
                      </el-popconfirm>
                      </template>
                    </div>
                    <el-divider></el-divider>

                  </div>
                </div>
              </div>
            </div>
          </div>
          <div style="text-align: right;margin: 10px 10px">
            <el-button type="primary" @click="tosave(item)" >评论</el-button>
          </div>

        </div>

      </div>

    </el-card>

    <el-dialog title="回复评论" :visible.sync="textareaFlag" width="50%" >
      <el-input type="textarea" v-model="CommentForm.content"  ></el-input>
      <div slot="footer" class="dialog-footer">
        <el-button @click="textareaFlag = false" class="ml-5">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>

export default {
  name: "Article",
  data() {
    return {
      tableData: {},
      name: '',
      CommentForm: {},
      content: '',
      comments: [],
      userContent: "",
      textareaFlag: false,
      show: false,
      id: this.$route.query.id,
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      itemId:'',
      nums:{},
      flag:false
    }
  },
  created() {
    this.load()
    this.loadComment()

  },
  methods: {
    pinglun() {
      this.show = true
    },
    load() {

      this.request.post("/article/getById/" + this.id).then(res => {
        this.tableData = res.data
        this.content = res.data.content
      })
    },
    loadComment() {
      this.request.post("/comment/getCommentUser/" + this.id).then(res => {
        this.comments = res.data
        console.log(this.comments)
        this.nums=res.data.id
        console.log(this.nums)
        this.userContent = this.comments.content
      })
    },
    userpinglun() {
      if (!this.user.id) {
        this.$message.warning("请登录再评论")
      }
      this.CommentForm.arcticleId = this.id
      this.request.post("/comment/insterzhu", this.CommentForm).then(res => {
        if (res.code === '200') {
          this.$message.success("评论成功")
          this.CommentForm = {} //初始化评论内容
          this.loadComment()
          this.textareaFlag=false
        } else {
          this.$message.error("评论失败")
        }
      })
    },
    save(){
      if (!this.user.id) {
        this.$message.warning("请登录再评论")
      }
      console.log(this.CommentForm.pid);
      console.log(this.CommentForm.originId)

        this.CommentForm.arcticleId = this.id
        this.request.post("/comment/inster", this.CommentForm).then(res => {
          if (res.code === '200') {
            this.$message.success("评论成功")
            this.CommentForm = {} //初始化评论内容
            this.loadComment()
            this.textareaFlag=false
          } else {
            this.$message.error("评论失败")
          }
        })
    },
    tosave(item){

      this.textareaFlag=true

      //如果获取的子评论的pid为空
      if(item.pid!=null){
        this.CommentForm={pid:item.id ,originId:item.id}

      }else{
        this.CommentForm={pid:item.id ,originId:item.id}
        this.flag=true
      }
    },
    del(id) {
      this.request.delete("/comment/" + id).then(res => {
        if (res.code == 200) {
          this.$message.success("删除成功")
          this.loadComment()
        } else {
          this.$message.error(res.msg)
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
